<?php
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$return=array();
$tabs=array(
	array('op'=>'list','title'=>'分销订单列表'),
	array('op'=>'add','title'=>'手动入单'),
);
set_html('tabs',$tabs);
if($_RQ['op']=='list'){
	$pindex = max(1, intval($_SESSION['page']));
	$psize = 15;
	$params=array('uniacid'=>$_SESSION['uniacid']);
	if($_RQ['mid']){
		$params['mid']=intval($_RQ['mid']);
		$return['params']['mid']=$_RQ['mid'];
	}
	if($_RQ['ordersn']){
		$params['ordersn like']='%'.trim($_RQ['ordersn']).'%';
		$return['params']['ordersn']=$_RQ['ordersn'];
	}
	$psize=20;
	$pindex=intval($_RQ['page'])?:1;
	$parameter=array(
		'params'=>$params,
		'limit'=>array($pindex,$psize),
		'orderby'=>'id desc',
	);
	$list=$this->get_list('order',$parameter,$total);
	if($list){
		foreach($list as &$item){
			$item['member']=cfc('core')->member_show($item['mid']);
			$item['detail']='/commission/list/ordersn='.$item['ordersn'];
			$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
			if($orderid){
				$item['isgoods']=1;
			}
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'export'=>'export',
		'list'=>array('data'=>'list','tip'=>'<red>[温馨提示]：只有存在上级，或者支持内购（属于分销商），才会产生佣金！</br>非必要，不建议删除记录，怕数据异常！</red>','edit'=>false,'del_one'=>true,'delall'=>true,'list'=>array(
			array('name'=>'member','title'=>'会员信息','type'=>'member'),
			array('name'=>'ordersn','title'=>'订单号','type'=>'string'),
			array('name'=>'order_price','title'=>'订单金额','type'=>'string'),
			array('name'=>'commission_price','title'=>'计算佣金的金额','type'=>'string'),
			array('name'=>'commission_money','title'=>'产生的佣金','type'=>'string'),
			array('name'=>'area_money','title'=>'社区代理分红','type'=>'string'),
			array('name'=>'jifen','title'=>'产生的积分','type'=>'string'),
			array('name'=>'xuni_money','title'=>'产生的'.$this->xuniname,'type'=>'string'),
			array('name'=>'createtime','title'=>'创建时间','type'=>'time'),
		),'action'=>array(
			array('type'=>'modal','op'=>'goods','title'=>'订单商品','show'=>array('isgoods'=>1)),
			array('type'=>'href','name'=>'detail','title'=>'佣金明细'),
		)),
		'search'=>array(
			array('type'=>'string','width'=>15,'name'=>'ordersn','title'=>'订单号'),
			array('type'=>'choose','width'=>15,'action'=>'core/member.get_member/','name'=>'mid','title'=>'选择会员'),
		),
	);
	$return['htmls']=$htmls;
	$return['levels']=$this->getall('level',array('uniacid'=>$_SESSION['uniacid']),array('id','title'),'id');
	$return['levels']['0']=array('id'=>'0','title'=>'默认等级');
	exi($return);
}
if($_RQ['op']=='goods'){
	$item=$this->get('order',array('id'=>intval($_RQ['id']),'uniacid'=>$_SESSION['uniacid']),array('id','ordersn'));
	if(!$item){
		exi('指定订单不存在！','error');
	}
	$orderid=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$item['ordersn'],'uniacid'=>$_SESSION['uniacid']),'id');
	if($orderid){
		$item=model('jy_weishop/order')->get_order($orderid);
	}
	$return['item']=$item;
	$return['htmls']=array(
		'type'=>'look',
		'desc'=>'商品清单',		
		'look'=>array(
			array('name'=>'goods','title'=>'商品信息','type'=>'array','list'=>array(
				array('type'=>'string','name'=>'goodsid','title'=>'商品ID'),
				array('type'=>'string','name'=>'title','title'=>'商品名称'),
				array('type'=>'image','name'=>'image','title'=>'商品图片'),
				array('type'=>'string','name'=>'price','title'=>'购买单价'),
				array('type'=>'string','name'=>'total','title'=>'购买数量'),
			)),
		)
	);
	exi($return);
}
if($_RQ['op']=='add'){
	if($_RQ['postdata']){
		$post=get_postdata();
		if(!$post['price']){
			exi('必须填写订单金额等','error');
		}
		if(!$post['mid']){
			exi('必须会员等','error');
		}
		$mid=$post['mid'];
		$agent_id=pdo_getcolumn('core_members',array('id'=>$mid,'uniacid'=>$_SESSION['uniacid']),'fromuser');
		if(!$agent_id){
			exi('该会员无上级，不能入单！','error');
		}
		$order=array(
			'mid'=>$mid,
			'price'=>$post['price'],
			'ordersn'=>'downup',
			'goods'=>array(),
		);
		$this->model('commission')->create($order);
		exi('生成订单成功！','','/order/');
	}
	$item=array();
	$return['item']=$item;
	$htmls=array(
		'type'=>'edit',
		'desc'=>'生成订单',
		'tabs'=>array(
			array('op'=>'list','title'=>'分销订单列表'),
		),
		'edit'=>array(
			array('title'=>'为了便于真实性，请选择真实的下单会员','type'=>'tip'),
			array('type'=>'choose','title'=>'选择会员','name'=>'mid','action'=>'core/member.get_member/','choose_title'=>'选择会员'),
			array('name'=>'price','title'=>'订单金额','type'=>'unitstring','unit'=>'元'),
		)
	);
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op'] == 'del'){
	$id=intval($_RQ['id']);
	if($id){
		$ordersn=pdo_getcolumn('jy_commission_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		$ordersn=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'ordersn');
		pdo_delete('jy_commission_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		if($ordersn){
			pdo_delete('jy_commission_commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
		foreach($del as $id){
			$id=intval($id);
			if($id){
				$ordersn=pdo_getcolumn('jy_commission_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']),'ordersn');
				$ordersn=pdo_getcolumn('jy_weishop_order',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']),'ordersn');
				pdo_delete('jy_commission_order',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
				if($ordersn){
					pdo_delete('jy_commission_commission',array('ordersn'=>$ordersn,'uniacid'=>$_SESSION['uniacid']));
				}
			}
		}
	}
	exi('删除成功！');
}
?>